Passed
Branch v5.x (e07a3a)
by Rafael S.
01:32
created

mulaw.js ➔ decodeSample   A

Complexity

Conditions 2
Paths 8

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 1
c 0
b 0
f 0
nc 8
rs 10
nop 1
1
/*
2
 * alawmulaw: A-Law and mu-Law codecs in JavaScript.
3
 * https://github.com/rochars/alawmulaw
4
 *
5
 * Copyright (c) 2018 Rafael da Silva Rocha.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 *
26
 */
27
28
/**
29
 * @fileoverview mu-Law codec.
30
 */
31
32
/** @module alawmulaw/mulaw */
33
34
/**
35
 * @type {number}
36
 * @private
37
 */
38
const BIAS = 0x84;
39
40
/**
41
 * Encode a 16-bit linear PCM sample as 8-bit mu-Law.
42
 * @param {number} sample A 16-bit PCM sample
43
 * @return {number}
44
 */
45
export function encodeSample(sample) {
46
  /** @type {number} */
47
  let mask = 0xFF;
48
  if (sample < 0) {
49
    sample = BIAS - sample;
50
    mask = 0x7F;
51
  } else {
52
    sample += BIAS;
53
  }
54
  if (sample > 0x7FFF) {
55
    sample = 0x7FFF;
56
  }
57
  /** @type {number} */
58
  let seg = segmentValue_(sample);
59
  /** @type {number} */
60
  let uval = (seg << 4) | ((sample >> (seg + 3)) & 0xF);
61
  return uval ^ mask;
62
}
63
64
/**
65
 * Decode a 8-bit mu-Law sample as 16-bit PCM.
66
 * @param {number} muLawSample The 8-bit mu-Law sample
67
 * @return {number}
68
 */
69
export function decodeSample(muLawSample) {
70
  muLawSample = ~muLawSample;
71
  /** @type {number} */
72
  let t = ((muLawSample & 0xf) << 3) + BIAS;
73
  t <<= (muLawSample & 0x70) >> 4;
74
  return ((muLawSample & 0x80) ? (BIAS - t) : (t - BIAS));
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
75
}
76
77
/**
78
 * Encode 16-bit linear PCM samples into 8-bit mu-Law samples.
79
 * @param {!Int16Array} samples A array of 16-bit PCM samples.
80
 * @return {!Uint8Array}
81
 */
82
export function encode(samples) {
83
  /** @type {!Uint8Array} */
84
  let muLawSamples = new Uint8Array(samples.length);
85
  for (let i=0; i<samples.length; i++) {
86
    muLawSamples[i] = encodeSample(samples[i]);
87
  }
88
  return muLawSamples;
89
}
90
91
/**
92
 * Decode 8-bit mu-Law samples into 16-bit PCM samples.
93
 * @param {!Uint8Array} samples A array of 8-bit mu-Law samples.
94
 * @return {!Int16Array}
95
 */
96
export function decode(samples) {
97
  /** @type {!Int16Array} */
98
  let pcmSamples = new Int16Array(samples.length);
99
  for (let i=0; i<samples.length; i++) {
100
    pcmSamples[i] = decodeSample(samples[i]);
101
  }
102
  return pcmSamples;
103
}
104
105
/**
106
 * Return the segment value of a PCM sample.
107
 * @param {number} sample
108
 * @return {number}
109
 * @private
110
 */
111
function segmentValue_(sample) {
112
  /** @type {number} */
113
  let segment = 0;
114
  sample >>= 7;
115
  if (sample & 0xf0) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
116
    sample >>= 4;
117
    segment += 4;
118
  }
119
  if (sample & 0x0c) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
120
    sample >>= 2;
121
    segment += 2;
122
  }
123
  if (sample & 0x02) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
124
    segment += 1;
125
  }
126
  return segment;
127
}
128